//
//  PlayerSettingController.m
//  SleepingElf
//
//  Created by karong on 2023/10/19.
//

#import "PlayerSettingController.h"
#import "PlayerSettingCell.h"

@interface PlayerSettingController ()
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *listViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainViewBottom;
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *bgArray;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *minLabel;
@property (weak, nonatomic) IBOutlet UIButton *autoCloseBtn;
@property (weak, nonatomic) IBOutlet UIButton *customBtn;
@property (nonatomic, strong) NSArray * dataArray;
@end

@implementation PlayerSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = appConfig.lastMusicArray;
    self.listViewHeight.constant = self.dataArray.count * (62 + 10) - 10;
    self.autoCloseBtn.selected = appConfig.sleepConfig.autoClose;
    [self.mainView uploadLayout];
    [self addObserver];
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:SESleepSettingNotification object:nil];
    
}

- (void)refreshData {
    [self layoutTimeBtn:appConfig.sleepConfig.autoCloseTimeType];
}

- (void)layoutTimeBtn:(NSInteger)type {
    if (type == 4) {
        NSInteger hour = appConfig.sleepConfig.autoCloseTime / 60;
        NSInteger min = appConfig.sleepConfig.autoCloseTime % 60;
        NSString * hourStr = hour>0?SESF(@"%ldh ", (long)hour):@"";
        NSString * minStr = min>0?SESF(@"%ldmin", (long)min):@"";
        NSString * timeStr = SESF(@"%@%@", hourStr, minStr);
        [self.customBtn setTitle:SEIsNullOrEmpty(timeStr)?WSLocalized(@"自定义"):timeStr forState:(UIControlStateNormal)];
    }
    else {
        [self.customBtn setTitle:WSLocalized(@"自定义") forState:(UIControlStateNormal)];
    }
    
    for (int i = 0; i < 4; i++) {
        UIImageView * image = self.bgArray[i];
        UILabel * label = self.minLabel[i];
        if (type == i) {
            image.image = SEIMG(@"bg_setTime_s");
            label.text = @"min";
        }
        else {
            image.image = SEIMG(@"bg_setTime_n");
            label.text = @"";
        }
    }
}

- (IBAction)closeAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)setTimeAction:(UIButton *)sender {
    [self layoutTimeBtn:sender.tag];
    appConfig.sleepConfig.autoCloseTimeType = sender.tag;
    NSArray * time = @[@"15",@"30",@"60",@"90"];
    appConfig.sleepConfig.autoCloseTime = [time[sender.tag] integerValue];
}

- (IBAction)autoCloseAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    appConfig.sleepConfig.autoClose = sender.selected;
}

- (IBAction)customAction:(id)sender {
    UIViewController * target = [self.storyboard instantiateViewControllerWithIdentifier:@"PlayerTimePicker"];
    [self presentViewController:target animated:YES completion:nil];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PlayerSettingCell * playerSettingCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlayerSettingCell" forIndexPath:indexPath];
    if (self.dataArray.count > 1) {
        [playerSettingCell setDataImageWithObject:self.dataArray[indexPath.row]];
    }
    else {
        [playerSettingCell setDataWithObject:self.dataArray[indexPath.row]];
    }
    return playerSettingCell;;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = SEScreenWidth;
    size.height = 62;
    return size;
}

@end
